home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / StringBuffer.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  41.9 KB  |  1,084 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)StringBuffer.java    1.45 98/10/27
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * A string buffer implements a mutable sequence of characters. 
  19.  * A string buffer is like a {@link String}, but can be modified. At any 
  20.  * point in time it contains some particular sequence of characters, but 
  21.  * the length and content of the sequence can be changed through certain 
  22.  * method calls.
  23.  * <p>
  24.  * String buffers are safe for use by multiple threads. The methods 
  25.  * are synchronized where necessary so that all the operations on any 
  26.  * particular instance behave as if they occur in some serial order 
  27.  * that is consistent with the order of the method calls made by each of 
  28.  * the individual threads involved. 
  29.  * <p>
  30.  * String buffers are used by the compiler to implement the binary 
  31.  * string concatenation operator <code>+</code>. For example, the code:
  32.  * <p><blockquote><pre>
  33.  *     x = "a" + 4 + "c"
  34.  * </pre></blockquote><p>
  35.  * is compiled to the equivalent of: 
  36.  * <p><blockquote><pre>
  37.  *     x = new StringBuffer().append("a").append(4).append("c")
  38.  *                           .toString()
  39.  * </pre></blockquote>
  40.  * which creates a new string buffer (initially empty), appends the string
  41.  * representation of each operand to the string buffer in turn, and then
  42.  * converts the contents of the string buffer to a string. Overall, this avoids
  43.  * creating many temporary strings.
  44.  * <p>
  45.  * The principal operations on a <code>StringBuffer</code> are the 
  46.  * <code>append</code> and <code>insert</code> methods, which are 
  47.  * overloaded so as to accept data of any type. Each effectively 
  48.  * converts a given datum to a string and then appends or inserts the 
  49.  * characters of that string to the string buffer. The 
  50.  * <code>append</code> method always adds these characters at the end 
  51.  * of the buffer; the <code>insert</code> method adds the characters at 
  52.  * a specified point. 
  53.  * <p>
  54.  * For example, if <code>z</code> refers to a string buffer object 
  55.  * whose current contents are "<code>start</code>", then 
  56.  * the method call <code>z.append("le")</code> would cause the string 
  57.  * buffer to contain "<code>startle</code>", whereas 
  58.  * <code>z.insert(4, "le")</code> would alter the string buffer to 
  59.  * contain "<code>starlet</code>". 
  60.  * <p>
  61.  * In general, if sb refers to an instance of a <code>StringBuffer</code>, 
  62.  * then <code>sb.append(x)</code> has the same effect as 
  63.  * <code>sb.insert(sb.length(), x)</code>.
  64.  * <p>
  65.  * Every string buffer has a capacity. As long as the length of the 
  66.  * character sequence contained in the string buffer does not exceed 
  67.  * the capacity, it is not necessary to allocate a new internal 
  68.  * buffer array. If the internal buffer overflows, it is 
  69.  * automatically made larger. 
  70.  *
  71.  * @author    Arthur van Hoff
  72.  * @version     1.45, 10/27/98
  73.  * @see     java.io.ByteArrayOutputStream
  74.  * @see     java.lang.String
  75.  * @since   JDK1.0
  76.  */
  77.  
  78. public final class StringBuffer implements java.io.Serializable {
  79.     /**
  80.      * The value is used for character storage.
  81.      * 
  82.      * @serial
  83.      */
  84.     private char value[];
  85.  
  86.     /** 
  87.      * The count is the number of characters in the buffer.
  88.      * 
  89.      * @serial
  90.      */
  91.     private int count;
  92.  
  93.     /**
  94.      * A flag indicating whether the buffer is shared 
  95.      *
  96.      * @serial
  97.      */
  98.     private boolean shared;
  99.  
  100.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  101.     static final long serialVersionUID = 3388685877147921107L;
  102.  
  103.     /**
  104.      * Constructs a string buffer with no characters in it and an 
  105.      * initial capacity of 16 characters. 
  106.      */
  107.     public StringBuffer() {
  108.     this(16);
  109.     }
  110.  
  111.     /**
  112.      * Constructs a string buffer with no characters in it and an 
  113.      * initial capacity specified by the <code>length</code> argument. 
  114.      *
  115.      * @param      length   the initial capacity.
  116.      * @exception  NegativeArraySizeException  if the <code>length</code>
  117.      *               argument is less than <code>0</code>.
  118.      */
  119.     public StringBuffer(int length) {
  120.     value = new char[length];
  121.     shared = false;
  122.     }
  123.  
  124.     /**
  125.      * Constructs a string buffer so that it represents the same 
  126.      * sequence of characters as the string argument; in other
  127.      * words, the initial contents of the string buffer is a copy of the 
  128.      * argument string. The initial capacity of the string buffer is 
  129.      * <code>16</code> plus the length of the string argument. 
  130.      *
  131.      * @param   str   the initial contents of the buffer.
  132.      */
  133.     public StringBuffer(String str) {
  134.     this(str.length() + 16);
  135.     append(str);
  136.     }
  137.  
  138.     /**
  139.      * Returns the length (character count) of this string buffer.
  140.      *
  141.      * @return  the length of the sequence of characters currently 
  142.      *          represented by this string buffer.
  143.      */
  144.     public int length() {
  145.     return count;
  146.     }
  147.  
  148.     /**
  149.      * Returns the current capacity of the String buffer. The capacity
  150.      * is the amount of storage available for newly inserted
  151.      * characters; beyond which an allocation will occur.
  152.      *
  153.      * @return  the current capacity of this string buffer.
  154.      */
  155.     public int capacity() {
  156.     return value.length;
  157.     }
  158.  
  159.     /**
  160.      * Copies the buffer value.  This is normally only called when shared
  161.      * is true.  It should only be called from a synchronized method.
  162.      */
  163.     private final void copy() {
  164.     char newValue[] = new char[value.length];
  165.     System.arraycopy(value, 0, newValue, 0, count);
  166.     value = newValue;
  167.     shared = false;
  168.     }
  169.  
  170.     /**
  171.      * Ensures that the capacity of the buffer is at least equal to the
  172.      * specified minimum.
  173.      * If the current capacity of this string buffer is less than the 
  174.      * argument, then a new internal buffer is allocated with greater 
  175.      * capacity. The new capacity is the larger of: 
  176.      * <ul>
  177.      * <li>The <code>minimumCapacity</code> argument. 
  178.      * <li>Twice the old capacity, plus <code>2</code>. 
  179.      * </ul>
  180.      * If the <code>minimumCapacity</code> argument is nonpositive, this
  181.      * method takes no action and simply returns.
  182.      *
  183.      * @param   minimumCapacity   the minimum desired capacity.
  184.      */
  185.     public synchronized void ensureCapacity(int minimumCapacity) {
  186.     if (minimumCapacity > value.length) {
  187.         expandCapacity(minimumCapacity);
  188.     }
  189.     }
  190.  
  191.     /**
  192.      * This implements the expansion semantics of ensureCapacity but is
  193.      * unsynchronized for use internally by methods which are already
  194.      * synchronized.
  195.      *
  196.      * @see java.lang.StringBuffer#ensureCapacity(int)
  197.      */
  198.     private void expandCapacity(int minimumCapacity) {
  199.     int newCapacity = (value.length + 1) * 2;
  200.     if (minimumCapacity > newCapacity) {
  201.         newCapacity = minimumCapacity;
  202.     }
  203.     
  204.     char newValue[] = new char[newCapacity];
  205.     System.arraycopy(value, 0, newValue, 0, count);
  206.     value = newValue;
  207.     shared = false;
  208.     }
  209.  
  210.     /**
  211.      * Sets the length of this String buffer.
  212.      * This string buffer is altered to represent a new character sequence 
  213.      * whose length is specified by the argument. For every nonnegative 
  214.      * index <i>k</i> less than <code>newLength</code>, the character at 
  215.      * index <i>k</i> in the new character sequence is the same as the 
  216.      * character at index <i>k</i> in the old sequence if <i>k</i> is less 
  217.      * than the length of the old character sequence; otherwise, it is the 
  218.      * null character <code>'\u0000'</code>. 
  219.      *  
  220.      * In other words, if the <code>newLength</code> argument is less than 
  221.      * the current length of the string buffer, the string buffer is 
  222.      * truncated to contain exactly the number of characters given by the 
  223.      * <code>newLength</code> argument. 
  224.      * <p>
  225.      * If the <code>newLength</code> argument is greater than or equal 
  226.      * to the current length, sufficient null characters 
  227.      * (<code>'\u0000'</code>) are appended to the string buffer so that 
  228.      * length becomes the <code>newLength</code> argument. 
  229.      * <p>
  230.      * The <code>newLength</code> argument must be greater than or equal 
  231.      * to <code>0</code>. 
  232.      *
  233.      * @param      newLength   the new length of the buffer.
  234.      * @exception  IndexOutOfBoundsException  if the
  235.      *               <code>newLength</code> argument is negative.
  236.      * @see        java.lang.StringBuffer#length()
  237.      */
  238.     public synchronized void setLength(int newLength) {
  239.     if (newLength < 0) {
  240.         throw new StringIndexOutOfBoundsException(newLength);
  241.     }
  242.     
  243.     if (newLength > value.length) {
  244.         expandCapacity(newLength);
  245.     }
  246.  
  247.     if (count < newLength) {
  248.         if (shared) copy();
  249.         for (; count < newLength; count++) {
  250.         value[count] = '\0';
  251.         }
  252.     } else {
  253.             count = newLength;
  254.             if (shared) copy();
  255.         }
  256.     }
  257.  
  258.     /**
  259.      * The specified character of the sequence currently represented by 
  260.      * the string buffer, as indicated by the <code>index</code> argument, 
  261.      * is returned. The first character of a string buffer is at index 
  262.      * <code>0</code>, the next at index <code>1</code>, and so on, for 
  263.      * array indexing. 
  264.      * <p>
  265.      * The index argument must be greater than or equal to 
  266.      * <code>0</code>, and less than the length of this string buffer. 
  267.      *
  268.      * @param      index   the index of the desired character.
  269.      * @return     the character at the specified index of this string buffer.
  270.      * @exception  IndexOutOfBoundsException  if <code>index</code> is 
  271.      *             negative or greater than or equal to <code>length()</code>.
  272.      * @see        java.lang.StringBuffer#length()
  273.      */
  274.     public synchronized char charAt(int index) {
  275.     if ((index < 0) || (index >= count)) {
  276.         throw new StringIndexOutOfBoundsException(index);
  277.     }
  278.     return value[index];
  279.     }
  280.  
  281.     /**
  282.      * Characters are copied from this string buffer into the 
  283.      * destination character array <code>dst</code>. The first character to 
  284.      * be copied is at index <code>srcBegin</code>; the last character to 
  285.      * be copied is at index <code>srcEnd-1</code>. The total number of 
  286.      * characters to be copied is <code>srcEnd-srcBegin</code>. The 
  287.      * characters are copied into the subarray of <code>dst</code> starting 
  288.      * at index <code>dstBegin</code> and ending at index:
  289.      * <p><blockquote><pre>
  290.      * dstbegin + (srcEnd-srcBegin) - 1
  291.      * </pre></blockquote>
  292.      *
  293.      * @param      srcBegin   start copying at this offset in the string buffer.
  294.      * @param      srcEnd     stop copying at this offset in the string buffer.
  295.      * @param      dst        the array to copy the data into.
  296.      * @param      dstBegin   offset into <code>dst</code>.
  297.      * @exception  NullPointerException if <code>dst</code> is 
  298.      *             <code>null</code>.
  299.      * @exception  IndexOutOfBoundsException  if any of the following is true:
  300.      *             <ul><li><code>srcBegin</code> is negative
  301.      *             <li>the <code>srcBeing</code> argument is greater than 
  302.      *             the <code>srcEnd</code> argument.
  303.      *             <li><code>srcEnd</code> is greater than 
  304.      *             <code>this.length()</code>, the current length of this 
  305.      *             string buffer.
  306.      *             <li><code>dstBegin+srcEnd-srcBegin</code> is greater than 
  307.      *             <code>dst.length</code></ul>
  308.      */
  309.     public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  310.     if ((srcBegin < 0) || (srcBegin >= count)) {
  311.         throw new StringIndexOutOfBoundsException(srcBegin);
  312.     }
  313.     if ((srcEnd < 0) || (srcEnd > count)) {
  314.         throw new StringIndexOutOfBoundsException(srcEnd);
  315.     }
  316.     if (srcBegin < srcEnd) {
  317.         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  318.     } else {
  319.         if (srcBegin > srcEnd) {
  320.         throw new StringIndexOutOfBoundsException
  321.             ("StringBuffer.getChars(): begin > end");
  322.         }
  323.         /* We do nothing when srcBegin == srcEnd. */
  324.     }
  325.     }
  326.  
  327.     /**
  328.      * The character at the specified index of this string buffer is set 
  329.      * to <code>ch</code>. The string buffer is altered to represent a new 
  330.      * character sequence that is identical to the old character sequence, 
  331.      * except that it contains the character <code>ch</code> at position 
  332.      * <code>index</code>. 
  333.      * <p>
  334.      * The offset argument must be greater than or equal to 
  335.      * <code>0</code>, and less than the length of this string buffer. 
  336.      *
  337.      * @param      index   the index of the character to modify.
  338.      * @param      ch      the new character.
  339.      * @exception  IndexOutOfBoundsException  if <code>index</code> is 
  340.      *             negative or greater than or equal to <code>length()</code>.
  341.      * @see        java.lang.StringBuffer#length()
  342.      */
  343.     public synchronized void setCharAt(int index, char ch) {
  344.     if ((index < 0) || (index >= count)) {
  345.         throw new StringIndexOutOfBoundsException(index);
  346.     }
  347.     if (shared) copy();
  348.     value[index] = ch;
  349.     }
  350.  
  351.     /**
  352.      * Appends the string representation of the <code>Object</code> 
  353.      * argument to this string buffer. 
  354.      * <p>
  355.      * The argument is converted to a string as if by the method 
  356.      * <code>String.valueOf</code>, and the characters of that 
  357.      * string are then appended to this string buffer. 
  358.      *
  359.      * @param   obj   an <code>Object</code>.
  360.      * @return  a reference to this <code>StringBuffer</code> object.
  361.      * @see     java.lang.String#valueOf(java.lang.Object)
  362.      * @see     java.lang.StringBuffer#append(java.lang.String)
  363.      */
  364.     public synchronized StringBuffer append(Object obj) {
  365.     return append(String.valueOf(obj));
  366.     }
  367.  
  368.     /**
  369.      * Appends the string to this string buffer. 
  370.      * <p>
  371.      * The characters of the <code>String</code> argument are appended, in 
  372.      * order, to the contents of this string buffer, increasing the 
  373.      * length of this string buffer by the length of the argument. 
  374.      * If <code>str</code> is <code>null</code>, then the four characters 
  375.      * <code>"null"</code> are appended to this string buffer.
  376.      * <p>
  377.      * Let <i>n</i> be the length of the old character sequence, the one 
  378.      * contained in the string buffer just prior to execution of the 
  379.      * <code>append</code> method. Then the character at index <i>k</i> in 
  380.      * the new character sequence is equal to the character at index <i>k</i> 
  381.      * in the old character sequence, if <i>k</i> is less than <i>n</i>; 
  382.      * otherwise, it is equal to the character at index <i>k-n</i> in the 
  383.      * argument <code>str</code>.
  384.      *
  385.      * @param   str   a string.
  386.      * @return  a reference to this <code>StringBuffer</code>.
  387.      */
  388.     public synchronized StringBuffer append(String str) {
  389.     if (str == null) {
  390.         str = String.valueOf(str);
  391.     }
  392.  
  393.     int len = str.length();
  394.     int newcount = count + len;
  395.     if (newcount > value.length)
  396.         expandCapacity(newcount);
  397.     str.getChars(0, len, value, count);
  398.     count = newcount;
  399.     return this;
  400.     }
  401.  
  402.     /**
  403.      * Appends the string representation of the <code>char</code> array 
  404.      * argument to this string buffer. 
  405.      * <p>
  406.      * The characters of the array argument are appended, in order, to 
  407.      * the contents of this string buffer. The length of this string 
  408.      * buffer increases by the length of the argument. 
  409.      * <p>
  410.      * The overall effect is exactly as if the argument were converted to 
  411.      * a string by the method {@link String#valueOf(char[])} and the 
  412.      * characters of that string were then {@link #append(String) appended} 
  413.      * to this <code>StringBuffer</code> object.
  414.      *
  415.      * @param   str   the characters to be appended.
  416.      * @return  a reference to this <code>StringBuffer</code> object.
  417.      */
  418.     public synchronized StringBuffer append(char str[]) {
  419.     int len = str.length;
  420.     int newcount = count + len;
  421.     if (newcount > value.length)
  422.         expandCapacity(newcount);
  423.     System.arraycopy(str, 0, value, count, len);
  424.     count = newcount;
  425.     return this;
  426.     }
  427.  
  428.     /**
  429.      * Appends the string representation of a subarray of the 
  430.      * <code>char</code> array argument to this string buffer. 
  431.      * <p>
  432.      * Characters of the character array <code>str</code>, starting at 
  433.      * index <code>offset</code>, are appended, in order, to the contents 
  434.      * of this string buffer. The length of this string buffer increases 
  435.      * by the value of <code>len</code>. 
  436.      * <p>
  437.      * The overall effect is exactly as if the arguments were converted to 
  438.      * a string by the method {@link String#valueOf(char[],int,int)} and the
  439.      * characters of that string were then {@link #append(String) appended} 
  440.      * to this <code>StringBuffer</code> object.
  441.      *
  442.      * @param   str      the characters to be appended.
  443.      * @param   offset   the index of the first character to append.
  444.      * @param   len      the number of characters to append.
  445.      * @return  a reference to this <code>StringBuffer</code> object.
  446.      */
  447.     public synchronized StringBuffer append(char str[], int offset, int len) {
  448.         int newcount = count + len;
  449.     if (newcount > value.length)
  450.         expandCapacity(newcount);
  451.     System.arraycopy(str, offset, value, count, len);
  452.     count = newcount;
  453.     return this;
  454.     }
  455.  
  456.     /**
  457.      * Appends the string representation of the <code>boolean</code> 
  458.      * argument to the string buffer. 
  459.      * <p>
  460.      * The argument is converted to a string as if by the method 
  461.      * <code>String.valueOf</code>, and the characters of that 
  462.      * string are then appended to this string buffer. 
  463.      *
  464.      * @param   b   a <code>boolean</code>.
  465.      * @return  a reference to this <code>StringBuffer</code>.
  466.      * @see     java.lang.String#valueOf(boolean)
  467.      * @see     java.lang.StringBuffer#append(java.lang.String)
  468.      */
  469.     public StringBuffer append(boolean b) {
  470.     return append(String.valueOf(b));
  471.     }
  472.  
  473.     /**
  474.      * Appends the string representation of the <code>char</code> 
  475.      * argument to this string buffer. 
  476.      * <p>
  477.      * The argument is appended to the contents of this string buffer. 
  478.      * The length of this string buffer increases by <code>1</code>. 
  479.      * <p>
  480.      * The overall effect is exactly as if the argument were converted to 
  481.      * a string by the method {@link String#valueOf(char)} and the character 
  482.      * in that string were then {@link #append(String) appended} to this 
  483.      * <code>StringBuffer</code> object.
  484.      *
  485.      * @param   ch   a <code>char</code>.
  486.      * @return  a reference to this <code>StringBuffer</code> object.
  487.      */
  488.     public synchronized StringBuffer append(char c) {
  489.         int newcount = count + 1;
  490.     if (newcount > value.length)
  491.         expandCapacity(newcount);
  492.     value[count++] = c;
  493.     return this;
  494.     }
  495.  
  496.     /**
  497.      * Appends the string representation of the <code>int</code> 
  498.      * argument to this string buffer. 
  499.      * <p>
  500.      * The argument is converted to a string as if by the method 
  501.      * <code>String.valueOf</code>, and the characters of that 
  502.      * string are then appended to this string buffer. 
  503.      *
  504.      * @param   i   an <code>int</code>.
  505.      * @return  a reference to this <code>StringBuffer</code> object.
  506.      * @see     java.lang.String#valueOf(int)
  507.      * @see     java.lang.StringBuffer#append(java.lang.String)
  508.      */
  509.     public StringBuffer append(int i) {
  510.     return append(String.valueOf(i));
  511.     }
  512.  
  513.     /**
  514.      * Appends the string representation of the <code>long</code> 
  515.      * argument to this string buffer. 
  516.      * <p>
  517.      * The argument is converted to a string as if by the method 
  518.      * <code>String.valueOf</code>, and the characters of that 
  519.      * string are then appended to this string buffer. 
  520.      *
  521.      * @param   l   a <code>long</code>.
  522.      * @return  a referenct to this <code>StringBuffer</code> object.
  523.      * @see     java.lang.String#valueOf(long)
  524.      * @see     java.lang.StringBuffer#append(java.lang.String)
  525.      */
  526.     public StringBuffer append(long l) {
  527.     return append(String.valueOf(l));
  528.     }
  529.  
  530.     /**
  531.      * Appends the string representation of the <code>float</code> 
  532.      * argument to this string buffer. 
  533.      * <p>
  534.      * The argument is converted to a string as if by the method 
  535.      * <code>String.valueOf</code>, and the characters of that 
  536.      * string are then appended to this string buffer. 
  537.      *
  538.      * @param   f   a <code>float</code>.
  539.      * @return  a reference to this <code>StringBuffer</code> object.
  540.      * @see     java.lang.String#valueOf(float)
  541.      * @see     java.lang.StringBuffer#append(java.lang.String)
  542.      */
  543.     public StringBuffer append(float f) {
  544.     return append(String.valueOf(f));
  545.     }
  546.  
  547.     /**
  548.      * Appends the string representation of the <code>double</code> 
  549.      * argument to this string buffer. 
  550.      * <p>
  551.      * The argument is converted to a string as if by the method 
  552.      * <code>String.valueOf</code>, and the characters of that 
  553.      * string are then appended to this string buffer. 
  554.      *
  555.      * @param   d   a <code>double</code>.
  556.      * @return  a reference to this <code>StringBuffer</code> object.
  557.      * @see     java.lang.String#valueOf(double)
  558.      * @see     java.lang.StringBuffer#append(java.lang.String)
  559.      */
  560.     public StringBuffer append(double d) {
  561.     return append(String.valueOf(d));
  562.     }
  563.  
  564.     /**
  565.      * Removes the characters in a substring of this <code>StringBuffer</code>.
  566.      * The substring begins at the specified <code>start</code> and extends to
  567.      * the character at index <code>end - 1</code> or to the end of the
  568.      * <code>StringBuffer</code> if no such character exists. If
  569.      * <code>start</code> is equal to <code>end</code>, no changes are made.
  570.      *
  571.      * @param      start  The beginning index, inclusive.
  572.      * @param      end    The ending index, exclusive.
  573.      * @return     This string buffer.
  574.      * @exception  StringIndexOutOfBoundsException  if <code>start</code>
  575.      *             is negative, greater than <code>length()</code>, or
  576.      *           greater than <code>end</code>.
  577.      * @since      JDK1.2
  578.      */
  579.     public synchronized StringBuffer delete(int start, int end) {
  580.     if (start < 0)
  581.         throw new StringIndexOutOfBoundsException(start);
  582.     if (end > count)
  583.         end = count;
  584.     if (start > end)
  585.         throw new StringIndexOutOfBoundsException();
  586.  
  587.         int len = end - start;
  588.         if (len > 0) {
  589.             if (shared)
  590.                 copy();
  591.             System.arraycopy(value, start+len, value, start, count-end);
  592.             count -= len;
  593.         }
  594.         return this;
  595.     }
  596.  
  597.     /**
  598.      * Removes the character at the specified position in this
  599.      * <code>StringBuffer</code> (shortening the <code>StringBuffer</code>
  600.      * by one character).
  601.      *
  602.      * @param       index  Index of character to remove
  603.      * @return      This string buffer.
  604.      * @exception   StringIndexOutOfBoundsException  if the <code>index</code>
  605.      *            is negative or greater than or equal to
  606.      *            <code>length()</code>.
  607.      * @since       JDK1.2
  608.      */
  609.     public synchronized StringBuffer deleteCharAt(int index) {
  610.         if ((index < 0) || (index >= count))
  611.         throw new StringIndexOutOfBoundsException();
  612.     if (shared)
  613.         copy();
  614.     System.arraycopy(value, index+1, value, index, count-index-1);
  615.     count--;
  616.         return this;
  617.     }
  618.  
  619.     /**
  620.      * Replaces the characters in a substring of this <code>StringBuffer</code>
  621.      * with characters in the specified <code>String</code>. The substring
  622.      * begins at the specified <code>start</code> and extends to the character
  623.      * at index <code>end - 1</code> or to the end of the
  624.      * <code>StringBuffer</code> if no such character exists. First the
  625.      * characters in the substring are removed and then the specified
  626.      * <code>String</code> is inserted at <code>start</code>. (The
  627.      * <code>StringBuffer</code> will be lengthened to accommodate the
  628.      * specified String if necessary.)
  629.      * 
  630.      * @param      start    The beginning index, inclusive.
  631.      * @param      end      The ending index, exclusive.
  632.      * @param      str   String that will replace previous contents.
  633.      * @return     This string buffer.
  634.      * @exception  StringIndexOutOfBoundsException  if <code>start</code>
  635.      *             is negative, greater than <code>length()</code>, or
  636.      *           greater than <code>end</code>.
  637.      * @since      JDK1.2
  638.      */ 
  639.     public synchronized StringBuffer replace(int start, int end, String str) {
  640.         if (start < 0)
  641.         throw new StringIndexOutOfBoundsException(start);
  642.     if (end > count)
  643.         end = count;
  644.     if (start > end)
  645.         throw new StringIndexOutOfBoundsException();
  646.  
  647.     int len = str.length();
  648.     int newCount = count + len - (end - start);
  649.     if (newCount > value.length)
  650.         expandCapacity(newCount);
  651.     else if (shared)
  652.         copy();
  653.  
  654.         System.arraycopy(value, end, value, start + len, count - end);
  655.         str.getChars(0, len, value, start);
  656.         count = newCount;
  657.         return this;
  658.     }
  659.  
  660.     /**
  661.      * Returns a new <code>String</code> that contains a subsequence of
  662.      * characters currently contained in this <code>StringBuffer</code>.The 
  663.      * substring begins at the specified index and extends to the end of the
  664.      * <code>StringBuffer</code>.
  665.      * 
  666.      * @param      start    The beginning index, inclusive.
  667.      * @return     The new string.
  668.      * @exception  StringIndexOutOfBoundsException  if <code>start</code> is
  669.      *             less than zero, or greater than the length of this
  670.      *             <code>StringBuffer</code>.
  671.      * @since      JDK1.2
  672.      */
  673.     public String substring(int start) {
  674.         return substring(start, count);
  675.     }
  676.  
  677.     /**
  678.      * Returns a new <code>String</code> that contains a subsequence of
  679.      * characters currently contained in this <code>StringBuffer</code>. The 
  680.      * substring begins at the specified <code>start</code> and 
  681.      * extends to the character at index <code>end - 1</code>. An
  682.      * exception is thrown if 
  683.      *
  684.      * @param      start    The beginning index, inclusive.
  685.      * @param      end      The ending index, exclusive.
  686.      * @return     The new string.
  687.      * @exception  StringIndexOutOfBoundsException  if <code>start</code>
  688.      *             or <code>end</code> are negative or greater than
  689.      *           <code>length()</code>, or <code>start</code> is
  690.      *           greater than <code>end</code>.
  691.      * @since      JDK1.2 
  692.      */
  693.     public synchronized String substring(int start, int end) {
  694.     if (start < 0)
  695.         throw new StringIndexOutOfBoundsException(start);
  696.     if (end > count)
  697.         throw new StringIndexOutOfBoundsException(end);
  698.     if (start > end)
  699.         throw new StringIndexOutOfBoundsException(end - start);
  700.         return new String(value, start, end - start);
  701.     }
  702.  
  703.     /**
  704.      * Inserts the string representation of a subarray of the <code>str</code>
  705.      * array argument into this string buffer. The subarray begins at the
  706.      * specified <code>offset</code> and extends <code>len</code> characters.
  707.      * The characters of the subarray are inserted into this string buffer at
  708.      * the position indicated by <code>index</code>. The length of this
  709.      * <code>StringBuffer</code> increases by <code>len</code> characters.
  710.      *
  711.      * @param      index    position at which to insert subarray.
  712.      * @param      str       A character array.
  713.      * @param      offset   the index of the first character in subarray to
  714.      *           to be inserted.
  715.      * @param      len      the number of characters in the subarray to
  716.      *           to be inserted.
  717.      * @return     This string buffer.
  718.      * @exception  StringIndexOutOfBoundsException  if <code>index</code>
  719.      *             is negative or greater than <code>length()</code>, or
  720.      *           <code>offset</code> or <code>len</code> are negative, or
  721.      *           <code>(offset+len)</code> is greater than
  722.      *           <code>str.length</code>.
  723.      * @since JDK1.2
  724.      */
  725.     public synchronized StringBuffer insert(int index, char str[], int offset,
  726.                                                                    int len) {
  727.         if ((index < 0) || (index > count))
  728.         throw new StringIndexOutOfBoundsException();
  729.     if ((offset < 0) || (offset + len > str.length))
  730.         throw new StringIndexOutOfBoundsException(offset);
  731.     if (len < 0)
  732.         throw new StringIndexOutOfBoundsException(len);
  733.     int newCount = count + len;
  734.     if (newCount > value.length)
  735.         expandCapacity(newCount);
  736.     else if (shared)
  737.         copy();
  738.     System.arraycopy(value, index, value, index + len, count - index);
  739.     System.arraycopy(str, offset, value, index, len);
  740.     count = newCount;
  741.     return this;
  742.     }
  743.  
  744.     /**
  745.      * Inserts the string representation of the <code>Object</code> 
  746.      * argument into this string buffer. 
  747.      * <p>
  748.      * The second argument is converted to a string as if by the method 
  749.      * <code>String.valueOf</code>, and the characters of that 
  750.      * string are then inserted into this string buffer at the indicated 
  751.      * offset. 
  752.      * <p>
  753.      * The offset argument must be greater than or equal to 
  754.      * <code>0</code>, and less than or equal to the length of this 
  755.      * string buffer. 
  756.      *
  757.      * @param      offset   the offset.
  758.      * @param      b        an <code>Object</code>.
  759.      * @return     a reference to this <code>StringBuffer</code> object.
  760.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  761.      * @see        java.lang.String#valueOf(java.lang.Object)
  762.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  763.      * @see        java.lang.StringBuffer#length()
  764.      */
  765.     public synchronized StringBuffer insert(int offset, Object obj) {
  766.     return insert(offset, String.valueOf(obj));
  767.     }
  768.  
  769.     /**
  770.      * Inserts the string into this string buffer. 
  771.      * <p>
  772.      * The characters of the <code>String</code> argument are inserted, in 
  773.      * order, into this string buffer at the indicated offset, moving up any 
  774.      * characters originally above that position and increasing the length 
  775.      * of this string buffer by the length of the argument. If 
  776.      * <code>str</code> is <code>null</code>, then the four characters 
  777.      * <code>"null"</code> are inserted into this string buffer.
  778.      * <p>
  779.      * The character at index <i>k</i> in the new character sequence is 
  780.      * equal to:
  781.      * <ul>
  782.      * <li>the character at index <i>k</i> in the old character sequence, if 
  783.      * <i>k</i> is less than <code>offset</code> 
  784.      * <li>the character at index <i>k</i><code>-offset</code> in the 
  785.      * argument <code>str</code>, if <i>k</i> is not less than 
  786.      * <code>offset</code> but is less than <code>offset+str.length()</code> 
  787.      * <li>the character at index <i>k</i><code>-str.length()</code> in the 
  788.      * old character sequence, if <i>k</i> is not less than 
  789.      * <code>offset+str.length()</code>
  790.      * </ul><p>
  791.      * The offset argument must be greater than or equal to 
  792.      * <code>0</code>, and less than or equal to the length of this 
  793.      * string buffer. 
  794.      *
  795.      * @param      offset   the offset.
  796.      * @param      str      a string.
  797.      * @return     a reference to this <code>StringBuffer</code> object.
  798.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  799.      * @see        java.lang.StringBuffer#length()
  800.      */
  801.     public synchronized StringBuffer insert(int offset, String str) {
  802.     if ((offset < 0) || (offset > count)) {
  803.         throw new StringIndexOutOfBoundsException();
  804.     }
  805.  
  806.     if (str == null) {
  807.         str = String.valueOf(str);
  808.     }
  809.     int len = str.length();
  810.     int newcount = count + len;
  811.     if (newcount > value.length)
  812.         expandCapacity(newcount);
  813.     else if (shared)
  814.         copy();
  815.     System.arraycopy(value, offset, value, offset + len, count - offset);
  816.     str.getChars(0, len, value, offset);
  817.     count = newcount;
  818.     return this;
  819.     }
  820.  
  821.     /**
  822.      * Inserts the string representation of the <code>char</code> array 
  823.      * argument into this string buffer. 
  824.      * <p>
  825.      * The characters of the array argument are inserted into the 
  826.      * contents of this string buffer at the position indicated by 
  827.      * <code>offset</code>. The length of this string buffer increases by 
  828.      * the length of the argument. 
  829.      * <p>
  830.      * The overall effect is exactly as if the argument were converted to 
  831.      * a string by the method {@link String#valueOf(char[])} and the 
  832.      * characters of that string were then 
  833.      * {@link #insert(int,String) inserted} into this 
  834.      * <code>StringBuffer</code>  object at the position indicated by
  835.      * <code>offset</code>.
  836.      *
  837.      * @param      offset   the offset.
  838.      * @param      ch       a character array.
  839.      * @return     a reference to this <code>StringBuffer</code> object.
  840.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  841.      */
  842.     public synchronized StringBuffer insert(int offset, char str[]) {
  843.     if ((offset < 0) || (offset > count)) {
  844.         throw new StringIndexOutOfBoundsException();
  845.     }
  846.     int len = str.length;
  847.     int newcount = count + len;
  848.     if (newcount > value.length)
  849.         expandCapacity(newcount);
  850.     else if (shared)
  851.         copy();
  852.     System.arraycopy(value, offset, value, offset + len, count - offset);
  853.     System.arraycopy(str, 0, value, offset, len);
  854.     count = newcount;
  855.     return this;
  856.     }
  857.  
  858.     /**
  859.      * Inserts the string representation of the <code>boolean</code> 
  860.      * argument into this string buffer. 
  861.      * <p>
  862.      * The second argument is converted to a string as if by the method 
  863.      * <code>String.valueOf</code>, and the characters of that 
  864.      * string are then inserted into this string buffer at the indicated 
  865.      * offset. 
  866.      * <p>
  867.      * The offset argument must be greater than or equal to 
  868.      * <code>0</code>, and less than or equal to the length of this 
  869.      * string buffer. 
  870.      *
  871.      * @param      offset   the offset.
  872.      * @param      b        a <code>boolean</code>.
  873.      * @return     a reference to this <code>StringBuffer</code> object.
  874.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  875.      * @see        java.lang.String#valueOf(boolean)
  876.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  877.      * @see        java.lang.StringBuffer#length()
  878.      */
  879.     public StringBuffer insert(int offset, boolean b) {
  880.     return insert(offset, String.valueOf(b));
  881.     }
  882.  
  883.     /**
  884.      * Inserts the string representation of the <code>char</code> 
  885.      * argument into this string buffer. 
  886.      * <p>
  887.      * The second argument is inserted into the contents of this string 
  888.      * buffer at the position indicated by <code>offset</code>. The length 
  889.      * of this string buffer increases by one. 
  890.      * <p>
  891.      * The overall effect is exactly as if the argument were converted to 
  892.      * a string by the method {@link String#valueOf(char)} and the character 
  893.      * in that string were then {@link #insert(int, String) inserted} into 
  894.      * this <code>StringBuffer</code> object at the position indicated by
  895.      * <code>offset</code>.
  896.      * <p>
  897.      * The offset argument must be greater than or equal to 
  898.      * <code>0</code>, and less than or equal to the length of this 
  899.      * string buffer. 
  900.      *
  901.      * @param      offset   the offset.
  902.      * @param      ch       a <code>char</code>.
  903.      * @return     a reference to this <code>StringBuffer</code> object.
  904.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  905.      * @see        java.lang.StringBuffer#length()
  906.      */
  907.     public synchronized StringBuffer insert(int offset, char c) {
  908.     int newcount = count + 1;
  909.     if (newcount > value.length)
  910.         expandCapacity(newcount);
  911.     else if (shared)
  912.         copy();
  913.     System.arraycopy(value, offset, value, offset + 1, count - offset);
  914.     value[offset] = c;
  915.     count = newcount;
  916.     return this;
  917.     }
  918.  
  919.     /**
  920.      * Inserts the string representation of the second <code>int</code> 
  921.      * argument into this string buffer. 
  922.      * <p>
  923.      * The second argument is converted to a string as if by the method 
  924.      * <code>String.valueOf</code>, and the characters of that 
  925.      * string are then inserted into this string buffer at the indicated 
  926.      * offset. 
  927.      * <p>
  928.      * The offset argument must be greater than or equal to 
  929.      * <code>0</code>, and less than or equal to the length of this 
  930.      * string buffer. 
  931.      *
  932.      * @param      offset   the offset.
  933.      * @param      b        an <code>int</code>.
  934.      * @return     a reference to this <code>StringBuffer</code> object.
  935.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  936.      * @see        java.lang.String#valueOf(int)
  937.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  938.      * @see        java.lang.StringBuffer#length()
  939.      */
  940.     public StringBuffer insert(int offset, int i) {
  941.     return insert(offset, String.valueOf(i));
  942.     }
  943.  
  944.     /**
  945.      * Inserts the string representation of the <code>long</code> 
  946.      * argument into this string buffer. 
  947.      * <p>
  948.      * The second argument is converted to a string as if by the method 
  949.      * <code>String.valueOf</code>, and the characters of that 
  950.      * string are then inserted into this string buffer at the position 
  951.      * indicated by <code>offset</code>. 
  952.      * <p>
  953.      * The offset argument must be greater than or equal to 
  954.      * <code>0</code>, and less than or equal to the length of this 
  955.      * string buffer. 
  956.      *
  957.      * @param      offset   the offset.
  958.      * @param      b        a <code>long</code>.
  959.      * @return     a reference to this <code>StringBuffer</code> object.
  960.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  961.      * @see        java.lang.String#valueOf(long)
  962.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  963.      * @see        java.lang.StringBuffer#length()
  964.      */
  965.     public StringBuffer insert(int offset, long l) {
  966.     return insert(offset, String.valueOf(l));
  967.     }
  968.  
  969.     /**
  970.      * Inserts the string representation of the <code>float</code> 
  971.      * argument into this string buffer. 
  972.      * <p>
  973.      * The second argument is converted to a string as if by the method 
  974.      * <code>String.valueOf</code>, and the characters of that 
  975.      * string are then inserted into this string buffer at the indicated 
  976.      * offset. 
  977.      * <p>
  978.      * The offset argument must be greater than or equal to 
  979.      * <code>0</code>, and less than or equal to the length of this 
  980.      * string buffer. 
  981.      *
  982.      * @param      offset   the offset.
  983.      * @param      b        a <code>float</code>.
  984.      * @return     a reference to this <code>StringBuffer</code> object.
  985.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  986.      * @see        java.lang.String#valueOf(float)
  987.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  988.      * @see        java.lang.StringBuffer#length()
  989.      */
  990.     public StringBuffer insert(int offset, float f) {
  991.     return insert(offset, String.valueOf(f));
  992.     }
  993.  
  994.     /**
  995.      * Inserts the string representation of the <code>double</code> 
  996.      * argument into this string buffer. 
  997.      * <p>
  998.      * The second argument is converted to a string as if by the method 
  999.      * <code>String.valueOf</code>, and the characters of that 
  1000.      * string are then inserted into this string buffer at the indicated 
  1001.      * offset. 
  1002.      * <p>
  1003.      * The offset argument must be greater than or equal to 
  1004.      * <code>0</code>, and less than or equal to the length of this 
  1005.      * string buffer. 
  1006.      *
  1007.      * @param      offset   the offset.
  1008.      * @param      b        a <code>double</code>.
  1009.      * @return     a reference to this <code>StringBuffer</code> object.
  1010.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  1011.      * @see        java.lang.String#valueOf(double)
  1012.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  1013.      * @see        java.lang.StringBuffer#length()
  1014.      */
  1015.     public StringBuffer insert(int offset, double d) {
  1016.     return insert(offset, String.valueOf(d));
  1017.     }
  1018.  
  1019.     /**
  1020.      * The character sequence contained in this string buffer is 
  1021.      * replaced by the reverse of the sequence. 
  1022.      * <p>
  1023.      * Let <i>n</i> be the length of the old character sequence, the one 
  1024.      * contained in the string buffer just prior to execution of the 
  1025.      * <code>reverse</code> method. Then the character at index <i>k</i> in 
  1026.      * the new character sequence is equal to the character at index 
  1027.      * <i>n-k-1</i> in the old character sequence.
  1028.      *
  1029.      * @return  a reference to this <codeStringBuffer</code> object..
  1030.      * @since   JDK1.0.2
  1031.      */
  1032.     public synchronized StringBuffer reverse() {
  1033.     if (shared) copy();
  1034.     int n = count - 1;
  1035.     for (int j = (n-1) >> 1; j >= 0; --j) {
  1036.         char temp = value[j];
  1037.         value[j] = value[n - j];
  1038.         value[n - j] = temp;
  1039.     }
  1040.     return this;
  1041.     }
  1042.  
  1043.     /**
  1044.      * Converts to a string representing the data in this string buffer.
  1045.      * A new <code>String</code> object is allocated and initialized to 
  1046.      * contain the character sequence currently represented by this 
  1047.      * string buffer. This <code>String</code> is then returned. Subsequent 
  1048.      * changes to the string buffer do not affect the contents of the 
  1049.      * <code>String</code>. 
  1050.      * <p>
  1051.      * Implementation advice: This method can be coded so as to create a new
  1052.      * <code>String</code> object without allocating new memory to hold a 
  1053.      * copy of the character sequence. Instead, the string can share the 
  1054.      * memory used by the string buffer. Any subsequent operation that alters 
  1055.      * the content or capacity of the string buffer must then make a copy of 
  1056.      * the internal buffer at that time. This strategy is effective for 
  1057.      * reducing the amount of memory allocated by a string concatenation 
  1058.      * operation when it is implemented using a string buffer.
  1059.      *
  1060.      * @return  a string representation of the string buffer.
  1061.      */
  1062.     public String toString() {
  1063.     return new String(this);
  1064.     }
  1065.  
  1066.     //
  1067.     // The following two methods are needed by String to efficiently
  1068.     // convert a StringBuffer into a String.  They are not public.
  1069.     // They shouldn't be called by anyone but String.
  1070.     final void setShared() { shared = true; } 
  1071.     final char[] getValue() { return value; }
  1072.  
  1073.     /**
  1074.      * readObject is called to restore the state of the StringBuffer from
  1075.      * a stream.
  1076.      */
  1077.     private void readObject(java.io.ObjectInputStream s)
  1078.          throws java.io.IOException, ClassNotFoundException {
  1079.     s.defaultReadObject();
  1080.     value = (char[]) value.clone();
  1081.     shared = false;
  1082.     }
  1083. }
  1084.